<?php
// 本类由系统自动生成，仅供测试用途
namespace Admin\Controller;
use Think\Controller;
class MyinfoController extends Controller {
  public function index(){
    $Myinfo = D('Myinfo');
    $res = $Myinfo->isGetMyinfo();
  }

  public function masa(){
     $this->Islogin();
      $Myinfo = D('Myinfo');
      $res = $Myinfo->getVehicle();
      if(!$res){
        $this->ajaxReturn(['error'=>'1','message'=>'该区域库存车辆缺货！']);die;
      }
      if(isset($res)){
        foreach ($res as $key => $value) {
          if(empty($value['carscolour'])){
            $value['transfer'] = "车辆待调";
            $res[$key] = $value;
          }
        }
      }
      // array_multisort(array_column($arr,'age'),SORT_DESC,$arr);
      $this->ajaxReturn(['success'=>'1','data'=>$res]);
  }
  /**
   * 临时车辆选择
   * @return [type] [description]
   */
  public function getAllVehicle(){
     // $this->Islogin();
      $Myinfo = D('Myinfo');
      $res = $Myinfo->getAllVehicle();
      $this->ajaxReturn(['success'=>'1','data'=>$res]);
  }
  public function GetInfo() {
    $this->Islogin();
    $myinfo_db = D('Myinfo');
    $field = [
      'usernum','insert_time','id','position','tel','name',
    ];
    $myinfo_data = $myinfo_db->getinfofield(['id'=>session('lampzuo_id')],$field);
    if($myinfo_data){
      echo json_encode(['success'=>'OK','data'=>$myinfo_data]);return;
    }else{
      echo json_encode(['error'=>'1','message'=>'数据获取失败请检查登录状态！']);return;
    }
  }

  public function getConfig(){
            $admin  = M('Admin');
            $interestrate   = M('interest_manage',null);
            $City   = M('City');
            $stock  = M('stock');
            $price  = M('price');
            $Carscolour   = M('cars_stock');
            $data   = $admin->where(array('id'=>9))->find();
            $result = $stock->where(array('is_show'=>1,'city_id'=>$data['stock_provinceid']))->select();
            if(!$result){return false;}
            $count1 = count($result);
            $city_arr=[];
            for($i = 0; $i < $count1; $i++){
                $sum1.=$sum1?",'".$result[$i]['id']."'":"'".$result[$i]['id']."'";
                $city_arr[$result[$i]['id']]=$result[$i];
            }
            $w  ="1=1";
            $w .=" and sid in (".$sum1.")";
            $resultadd = $Carscolour->where($w)->order('stock desc')->select();
            $color_arr=[];
            $stock_arr=[];
            foreach ($resultadd as $key => $value) {
                $stock_arr[$resultadd[$key]['carid']]["stock"] +=$value['stock'];
                $resultadd[$key]['name'] = $city_arr[$value['sid']]['city'];
                $color_arr[$resultadd[$key]['carid']][$value["id"]]=$resultadd[$key];
                $sum2.=$sum2?",'".$value['carid']."'":"'".$value['carid']."'";
            }
            $brandDb=M('s_sjzd', null);
            $where = ['type' => '品牌','isdel'=>'0'];
            $brandData = $brandDb -> where($where) -> field('id, content as brand') -> select();
            $brands = [];
            foreach ($brandData as $k => $v) {
                if (!in_array(['brand' => $v['brand']], $brands)) {
                    $brands[$v['id']] = ['brand' => $v['brand']];
                }
            }
            $datas['brand']=$brands;
            $cars=M('qcgl_manage', null);
            $where="1=1";
            $where.=" and id in (".$sum2.")";
            $carData=$cars ->where($where)-> field('id, cs, qcmc, cszdj, ppid,xsjg,yg,sf,ishdcx') -> select();
            foreach($carData as $val){
                $arr1=$color_arr[$val['id']];
                $val['stockcount']  = $stock_arr[$val['id']]['stock'];
                foreach($arr1 as $v){
                      $pricestock = $cars->where(array('id'=>$v['carid']))->field('id, cs, qcmc, cszdj, ppid,xsjg,yg,sf')->find();
                      $rate = $interestrate->select();
                      $v['price'] = $pricestock;
                      $v['rate'] = $rate;
                      if(in_array($v['color'],$arr2)){
                         $k=array_search($v['color'],$arr2);
                         $objarr[$k]['city'][]=$v;
                      }else{
                          $arr2[]=$v['color'];
                          $obj=[];
                          $obj['colorname']=$v['color'];
                          if($keys){
                              $obj['city'][$v['id']]=$v;
                          }else{
                              $obj['city'][]=$v;
                          }
                          $objarr[]=$obj;
                      }
                }
                $val['colorarr']=$objarr;
                if($keys){
                    $carcolor_arr[$val['id']]=$val;
                }else{
                    $carcolor_arr[]=$val;
                } 
                // /================================
                $datas['cars'][$val['ppid']][]=$val;
            }
            $this -> ajaxReturn($datas, 'json');
  }
  
}